﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Anthem;
using spUtils;
using spMapping;
using spData.Common;


namespace demo.Page.SJ
{
    public partial class sj_Clsbxx : TaskPage
    {
        #region Property
        //初始化下拉
        spWeb.DDLBind db = new spWeb.DDLBind();
        #endregion

        #region Event Page
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region Event Save As Draft
        protected void SaveAsDraft_Click(object sender, EventArgs e)
        {
            bool success = Submit(false);
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}')", success ? "保存成功!" : "保存失败!"));
        }
        #endregion

        #region Event Send
        protected void Send_Click(object sender, EventArgs e)
        {
            List<string> msg = DataValidation();
            if (msg.Count > 0)
            {
                Anthem.Manager.AddScriptForClientSideEval(Alert(msg));
                return;
            }

            bool success = Submit(true);
            (sender as spWeb.Controls.AssessmentExt).Refresh();
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}');{1}", success ? "提交成功!" : "提交失败!", success ? "window.close();" : ""));
        }
        #endregion

        #region Event Aborted
        protected void Aborted_Click(object sender, EventArgs e)
        {
            (cg_CGLXD.process == TaskAPI.Process & cg_CGLXD.incident == TaskAPI.Incident).Edit<cg_CGLXD>(p => { p.Status = 4; });
        }
        #endregion

        #region Event City(城市公司) & Gsgs(开发公司)
        protected void City_TextChanged(object sender, EventArgs e)
        {
            db.BindGsgs(Gsgs, City.SelectedValue);
        }
        protected void Gsgsbmkfgs_TextChanged(object sender, EventArgs e)
        {
            db.BindXmmc(Xmmc, Gsgs.SelectedValue);
        }
        #endregion

        #region Method Init Controls Value & Show/Hide & Enable/Disable & Opinion
        private void InitControls()
        {
            #region Init controls value
            //绑定城市
            db.BindCity(City);

            Expression exp = new Expression();
            string did = Request.QueryString["DID"];
            exp = string.IsNullOrEmpty(did) ? dc_sj_Clsbxx.process == TaskAPI.Process & dc_sj_Clsbxx.incident == TaskAPI.Incident : dc_sj_Clsbxx.id == did;
            if (TaskAPI.IsInitiate)
            {
                exp &= dc_sj_Clsbxx.userName == USER.UserName;
            }

            dc_sj_Clsbxx v = exp.ListFirst<dc_sj_Clsbxx>();
            if (v != null)
            {
                SetControlsValue(v);
                DepEName.Text = v.Department;
                //DID方式加载
                if (!string.IsNullOrEmpty(did))
                {
                    TaskAPI.LoadByDID(v.Process, v.Incident);
                    Process = v.Process;
                    Incident = v.Incident;
                }
            }
            else
            {
                UserFullName.Text = USER.UserFullName;
                Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                Department.Text = USER.Department;
                DepEName.Text = USER.DepEName;
                Tel.Text = USER.EXTEND.CellPhone;
            }
            #endregion

            #region Init Show/Hide、Enable/Disable
            bool flg = true;//TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;

            //SetEnable(flg, Tel, City, Gsgs, Xmmc, Jzxtgc, Jzxtbs, Jzxtdc, Jzxtsy, Bben, Xmsjjh, Bz);

            SetVisible(flg, R_Gsgs, R_Xmmc, R_Bz, R_Jzxt, R_Bben, R_Xmjh);
            #endregion

        }
        #endregion

        #region Method Data Validation
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            //if (!AssExt.IsConferWithBack && !AssExt.IsForwardWithBack)
            //{
            if (TaskAPI.IsBeginStep)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, City.Text, "“城市公司”为必填项!"));
                regs.Add(new Regular(RegularExp.NotEmpty, Gsgs.Text, "“开发公司”为必填项!"));
                regs.Add(new Regular(RegularExp.NotEmpty, Xmmc.Text, "“项目名称”为必填项!"));
                regs.Add(new Regular(RegularExp.NotEmpty, IsDecorated.SelectedItem.Text, "“是否精装修”为必填项!"));
                regs.Add(new Regular(RegularExp.NotEmpty, SelectionType.SelectedItem.Text, "“选型类型”为必填项!"));
                //regs.Add(new Regular(RegularExp.NotEmpty, Bben.Text, "“版本”为必填项!"));
                //regs.Add(new Regular(RegularExp.NotEmpty, Attachment1.IsHaveAttachment ? "" : "SKIP", "“项目总体计划附件必须上传!"));
            }
            //}
            return Validation(regs);
        }
        #endregion

        #region Method Engine Transfer Value
        private void EngineTransferValue()
        {
            if (TaskAPI.IsBeginStep)
            {
                TaskAPI.Summary = Xmmc.Text;
                spWeb.WebHelper.IntSetValue(this.TaskAPI, Gsgs.SelectedValue, Xmmc.SelectedValue);

            }

            //TaskAPI["e_Approve"] = AssExt.Assessment.Opinion;
        }

        #endregion

        #region Method Submit
        private bool Submit(bool IsSend)
        {
            #region “开始”步骤
            if (TaskAPI.IsBeginStep)
            {
                dc_sj_Clsbxx v = (dc_sj_Clsbxx.process == TaskAPI.Process & dc_sj_Clsbxx.incident == TaskAPI.Incident
                                & dc_sj_Clsbxx.userName == (TaskAPI.IsInitiate ? USER.UserName : TaskAPI.UserName)).ListFirst<dc_sj_Clsbxx>();
                bool result = GetControlsValue<dc_sj_Clsbxx>(ref v);
                if (result)
                {
                    v.Process = TaskAPI.Process;
                    v.Incident = TaskAPI.Incident;
                    v.UserName = USER.UserName;
                    v.UserFullName = USER.UserFullName;
                    v.Time = System.DateTime.Now;
                    //ApplicationNo.Text = GetNewSequence("DCCG-XMZTCGJH-" + DateTime.Now.ToString("yyyyMMdd") + "-", "", 3);
                    v.ApplicationNo = ApplicationNo.Text;
                }
                ORM.Save(v);


            }

            if (TaskAPI.Step == "")
            {

            }
            #endregion

            if (IsSend)
            {
                EngineTransferValue();
                //ORM.Save(AssExt.Assessment);
                TaskAPI.Send();
            }

            if (IsSend && TaskAPI.IsInitiate)
            {
                (dc_sj_Clsbxx.process == TaskAPI.Process & dc_sj_Clsbxx.incident == 0 & dc_sj_Clsbxx.userName == USER.UserName).Edit<dc_sj_Clsbxx>(p => p.Incident = TaskAPI.Incident);
            }

            //退出
            if (IsSend)
            {
                Exit();
            }
            return true;
        }
        #endregion
    }
}